package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.dto.ShoppingCarAddNewDTO;
import cn.scxy.bookcity.pojo.dto.UserLoginDTO;
import cn.scxy.bookcity.pojo.vo.shoppingCarVO;
import cn.scxy.bookcity.service.IShoppingCarService;
import cn.scxy.bookcity.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/shoppingCar")
@Api(tags = "01. 购物车管理模块")
public class ShoppingCarController {
    @Autowired
    private IShoppingCarService mapper;


    public ShoppingCarController() {
        log.debug("创建控制器购物车模块");
    }

    @ApiOperation("查询列表")
    @ApiOperationSupport(order = 100)
    @GetMapping("/shoppinglist/{uid}")
    public JsonResult<List<shoppingCarVO>> list(@PathVariable Long uid) {
        log.debug("处理查询购物车请求");
        List<shoppingCarVO> list = mapper.list(uid);
        return JsonResult.ok(list);
    }


    @ApiOperation("添加购物车商品")
    @ApiOperationSupport(order = 200)
    @PostMapping("/insert")
    public JsonResult<UserLoginDTO> insert(ShoppingCarAddNewDTO shoppingCarAddNewDTO , HttpSession session) {
        log.debug("接收到:{}",shoppingCarAddNewDTO);
        UserLoginDTO user = (UserLoginDTO) session.getAttribute("user");
        if (user==null){
            return JsonResult.ok(user);
        }
        mapper.insert(shoppingCarAddNewDTO);
        return JsonResult.ok(user);
    }

    @ApiOperation("查询当前登录用户")
    @ApiOperationSupport(order = 305)
    @GetMapping("/getUserLogin")
    public JsonResult<UserLoginDTO> getUserLogin(HttpSession session){
        log.debug("查询当前登录用户");
        UserLoginDTO user = (UserLoginDTO) session.getAttribute("user");
        log.debug("获取到的值:{}",user);
        return JsonResult.ok(user);
    }

    @ApiOperation("删除购物车商品")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除购物车失败，尝试删除的购物车的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除购物车】的请求，参数：{}", id);
        mapper.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("加入订单后删除购物车商品群")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @PostMapping("/{ids:[0-9]+}/delete1")
    public JsonResult<Void> delete1(@Range(min = 1, message = "删除购物车失败，尝试删除的购物车的ID无效！")
                                   @PathVariable Long[] ids) {
        log.debug("开始处理【根据id删除购物车】的请求，参数：{}", ids);
        mapper.deleteByIds(ids);
        return JsonResult.ok();
    }
}
